<template>
  <div>
    <BasicModal
      @register="registerModal"
      v-bind="$attrs"
      @ok="handleSubmit"
      title="编辑资产"
      ok-text="保存"
      :closeFunc="handleClose"
    >
      <BasicForm @register="registerForm" />
    </BasicModal>
  </div>
</template>

<script setup lang="ts">
  import { ref, defineEmits } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form';
  import { assetSchemas } from './assetModal.data';
  import { getAssetInfo } from '/@/api/thingsborad/entity/entity';
  import { modalFormStyleSetting, modalStyleSetting } from '/@/settings/thingsBoardDesign';
  let rowId = ref(0);
  let assetInfo = {};
  const emit = defineEmits(['success', 'register']);
  const [registerForm, { validate, setFieldsValue, resetFields }] = useForm({
    layout: 'vertical',
    schemas: assetSchemas,
    showActionButtonGroup: false,
    ...modalFormStyleSetting,
  });
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    setModalProps({ loading: true, confirmLoading: true, ...modalStyleSetting });
    rowId.value = data?.record?.aasetProfileId?.id;
    assetInfo = await getAssetInfo(data?.record?.id);
    rowId.value = data?.record?.id;
    setFieldsValue({
      ...data.record,
      assetConfiguration: JSON.stringify(data.record.assetProfileId),
      remark: data?.record?.additionalInfo?.description,
    });
    setModalProps({ loading: false, confirmLoading: false });
  });
  async function handleClose() {
    resetFields();
    return true;
  }
  async function handleSubmit() {
    const values = await validate();
    setModalProps({ confirmLoading: true });
    closeModal();
    emit('success', { key: 'edit', record: { ...assetInfo }, values: { ...values } });
    setModalProps({ confirmLoading: false });
  }
</script>
